package com.example.satoken.util;

import org.springframework.util.Assert;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author wpz
 */
public class FormValidatorUtils {

    /**
     * email
     */
    private static final String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@" + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";


    /**
     * phone
     */
    private static final String PHONE_PATTERN = "^1[3|4|5|6|7|8][0-9]\\d{8}$";


    /**
     * QQ
     */
    private static final String QQ_PATTERN = "^[1-9][0-9]{4,10}$";

    private static final Pattern EMAIL_REGEX = Pattern.compile(EMAIL_PATTERN);

    private static final Pattern PHONE_REGEX = Pattern.compile(PHONE_PATTERN);

    private static final Pattern QQ_REGEX = Pattern.compile(QQ_PATTERN);

    /**
     * @param email
     * @return
     */
    public static boolean validateEmail(String email) {
        Assert.notNull(email, "values is empty");
        Matcher matcher = EMAIL_REGEX.matcher(email);
        return matcher.matches();
    }

    /**
     * @param phone
     * @return
     */
    public static boolean validatePhone(String phone) {
        Assert.notNull(phone, "values is empty");
        Matcher matcher = PHONE_REGEX.matcher(phone);
        return matcher.matches();
    }

    /**
     * @param qq
     * @return
     */
    public static boolean validateQQ(String qq) {
        Assert.notNull(qq, "values is empty");
        Matcher matcher = QQ_REGEX.matcher(qq);
        return matcher.matches();
    }
}